<?php

namespace app\api\controller;

use app\common\traits\ApiTrait;
use support\Request;
use support\Response;
use think\facade\Db;
use app\extend\WxUtil\WxTool;
use Tinywan\Jwt\JwtToken;

class ClerkController
{
    use ApiTrait;

    /**
     * 加入店员
     * @param Request $request
     * @return Response
     * @throws \think\db\exception\DbException
     */
    public function join(Request $request) : Response
    {
        if (!request()->isPost()) {
            return $this->reError('请求方式错误');
        }

        $jwt = JwtToken::getExtend();
        $activityId = $request->post('activityId');
        $staffUserId = $request->post('staffUserId');

        $user = Db::name('user')->where(['id' => $jwt['id']])->find();
        if (empty($user)) {
            return $this->reError('账户异常');
        }

        // 判断是否有活动角色数据
        $user_role = Db::name('activity_user_role')->where(['activity_id' => $activityId, 'user_id' => JwtToken::getExtend()['id']])->find();
        if (empty($user_role)) {
            Db::name('activity_user_role')->insert(['activity_id' => $activityId, 'user_id' => JwtToken::getExtend()['id'], 'role' => 0]);
        }

        // 判断是否有加入资格
        $role = Db::name('activity_user_role')->where(['activity_id' => $activityId, 'user_id' => JwtToken::getExtend()['id']])->value('role', 0);
        if ($role == 0) {
            Db::name('activity_user_role')->where(['user_id' => $jwt['id'], 'activity_id' => $activityId])->update(['role' => 2, 'staff_user_id' => $staffUserId, 'create_time' => date('Y-m-d H:i:s')]);
            return $this->reSuccess('加入成功');
        } else if ($role == 1) {
            return $this->reError("您是商家，不可成为店员", false);
        } else if ($role == 2) {
            return $this->reError('您已经是店员', false);
        } else {
            return $this->reError('角色异常', false);
        }
    }

    /**
     * 溯源订单
     * @param Request $request
     * @return Response
     */
    public function sourceOrder(Request $request) : Response
    {
        if (!request()->isGet()) {
            return $this->reError('请求方式错误');
        }

        $activityId = $request->get('activityId');
        if (empty($activityId) || $activityId == 'false') {
            return $this->reError('请重新进入活动');
        }

        $status = $request->get('status');
        $page = $request->get('page');
        $limit = $request->get('limit');
        if ($status == 1) {
            $where = [['a.order_status', 'IN', [0 , 1]]];
        } else if ($status == 2) {
            $where = [['a.order_status', '=', 2]];
        }
        $data = Db::name('order')
            ->alias('a')
            ->join('user b', 'a.user_id = b.id')
            ->where($where ?? [])
            ->where(['a.activity_id' => $activityId, 'a.clerk_user_id' => JwtToken::getExtend()['id']])
            ->field('a.*, b.nickname, b.avatar_url')
            ->order('a.id desc')->page($page, $limit)->select()->toArray();
        return $this->reSuccess('数据获取成功', $data);
    }

    /**
     * 推荐订单
     * @param Request $request
     * @return Response
     */
    public function recomOrder(Request $request) : Response
    {
        if (!request()->isGet()) {
            return $this->reError('请求方式错误');
        }

        $activityId = $request->get('activityId');
        if (empty($activityId) || $activityId == 'false') {
            return $this->reError('请重新进入活动');
        }

        $page = $request->get('page');
        $limit = $request->get('limit');
        $data = Db::name('order')
            ->alias('a')
            ->join('user b', 'a.user_id = b.id')
            ->where(['a.activity_id' => $activityId, 'a.recom_user_id' => JwtToken::getExtend()['id']])
            ->field('a.*, b.nickname, b.avatar_url')
            ->order('a.id desc')->page($page, $limit)->select()->toArray();
        return $this->reSuccess('数据获取成功', $data);
    }

    /**
     * 核销
     * @return void
     */
    public function confirmPrize(Request $request) : Response
    {
        if (!request()->isPost()) {
            return $this->reError('请求方式错误');
        }

        $prizeId = $request->post('prizeId');

        switch ($request->post('type')) {
            // 翻牌抽奖奖品
            case 1:
                // 判断是否有核销资格
                $qualification = Db::name('payment_activity_prize')
                    ->alias('a')
                    ->join('activity b', 'a.activity_id = b.id')
                    ->join('activity_user_role c', 'b.id = c.activity_id')
                    ->where(['c.user_id' => JwtToken::getExtend()['id'], 'c.role' => 2, 'a.id' => $prizeId])
                    ->find();
                if (!empty($qualification)) {
                    // 必须领取后才能核销
                    $result = Db::name('payment_activity_prize')
                        ->where(['id' => $prizeId, 'receive_status' => 1])
                        ->update(['operator_user_id' => JwtToken::getExtend()['id'], 'status' => 2, 'confirm_time' => date('Y-m-d H:i:s')]);
                }
                break;
            // 平台盲盒奖品
            case 2:
                // 判断是否有核销资格
                $qualification = Db::name('payment_platform_box')
                    ->alias('a')
                    ->join('activity b', 'a.activity_id = b.id')
                    ->join('activity_user_role c', 'b.id = c.activity_id')
                    ->where(['c.user_id' => JwtToken::getExtend()['id'], 'c.role' => 2, 'a.id' => $prizeId])
                    ->find();
                if (!empty($qualification)) {
                    // 核销
                    $result = Db::name('payment_platform_box')
                        ->where(['id' => $prizeId])
                        ->update(['operator_user_id' => JwtToken::getExtend()['id'], 'status' => 2, 'confirm_time' => date('Y-m-d H:i:s')]);
                }
                break;
        }
        return (!empty($result)) ? $this->reSuccess('核销成功') : $this->reError('核销失败', false);
    }

    /**
     * 核销记录
     * @return void
     */
    public function confirmPrizeList(Request $request) : Response
    {
        if (!request()->isPost()) {
            return $this->reError('请求方式错误');
        }

        $activityId = $request->post('activityId');
        if (empty($activityId) || $activityId == 'false') {
            return $this->reError('请重新进入活动');
        }

        $type = $request->post('type');
        $page = $request->post('page');
        $limit = $request->post('limit');

        switch ($type) {
            case 1:
                // 翻牌奖品
                $data = Db::name('payment_activity_prize')
                    ->alias('a')
                    ->join('activity_prize b', 'a.activity_prize_id = b.id')
                    ->join('user c', 'a.user_id = c.id')
                    ->join('order d', 'a.order_id = d.id')
                    ->join('user e', 'a.operator_user_id = e.id', 'left')
                    ->where([['a.activity_id', '=', $activityId], ['a.status', '=', 2], ['operator_user_id', '=', JwtToken::getExtend()['id']]])
                    ->field('a.*, d.order_sn, c.nickname, c.avatar_url, d.name, d.phone, e.nickname as operator_nickname')
                    ->order('a.id desc')
                    ->page($page, $limit)->select()->toArray();
                break;
            case 2:
                // 盲盒奖品
                $data = Db::name('payment_platform_box')
                    ->alias('a')
                    ->join('platform_box_prize b', 'a.platform_box_prize_id = b.id')
                    ->join('user c', 'a.user_id = c.id')
                    ->join('order d', 'a.order_id = d.id')
                    ->join('user e', 'a.operator_user_id = e.id', 'left')
                    ->where([['a.activity_id', '=', $activityId], ['a.status', '=', 2], ['operator_user_id', '=', JwtToken::getExtend()['id']]])
                    ->field('a.*, d.order_sn, c.nickname, c.avatar_url, d.name, d.phone, e.nickname as operator_nickname')
                    ->order('a.id desc')
                    ->page($page, $limit)->select()->toArray();
                break;
        }
        return $this->reSuccess('数据获取成功', $data);
    }
}
